package config

import (
	"fmt"
	"gopkg.in/gomail.v2"
	"log"
	"tg-bot/model"
)

var smtp *gomail.Dialer
var configEmail model.ConfigEmail

func buildEmailAuth(email model.ConfigEmail) {
	smtp = gomail.NewDialer(email.Host, email.Port, email.Account, email.Password)
	configEmail = email
}

func SendEmailMessage(emailModel SendEmailModel) {
	mailer := gomail.NewMessage()
	mailer.SetHeader("From", fmt.Sprintf("\"%s\" <%s>", configEmail.Personal, configEmail.Account))
	mailer.SetHeader("To", emailModel.Account)
	mailer.SetHeader("Subject", emailModel.Subject)

	mailer.SetBody("text/html", emailModel.Body)

	if err := smtp.DialAndSend(mailer); err != nil {
		log.Fatalf("发送邮箱 信息失败 %v", err)
	}
}

type SendEmailModel struct {
	Account     string `json:"account"`     // 接受方账号
	Subject     string `json:"subject"`     // 邮箱主题
	Body        string `json:"body"`        // 发件内容
	RedisKey    string `json:"redisKey"`    // redis 缓存键
	VerifyEmail bool   `json:"verifyEmail"` // 是否为验证邮箱
	CreateTime  int64  `json:"createTime"`  // 请求发送邮箱
	Status      int    `json:"status"`      // 邮箱状态
}
